﻿using Magic.Core;
using Magic.Core.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ManagementTools.Service;

namespace ManagementTools
{
    public partial class FrmDicToEnum : Form
    {
        List<SysDictTypeDto> mDicts;
        List<SysDictData> mDictDatas;
        DicToEnumService mDicToEnumService;
        SqlSugarClient mSugar;
        public FrmDicToEnum(SqlSugarClient sugarClient)
        {
            mSugar= sugarClient;
            mDicToEnumService=new DicToEnumService(sugarClient);
            InitializeComponent();
        }

        private void FrmDicToEnum_Load(object sender, EventArgs e)
        {
            dgvDic.AutoGenerateColumns = false;
            dgvValue.AutoGenerateColumns = false;
           
            btnGetDic_Click(sender, e);
        }
        private void btnGetDic_Click(object sender, EventArgs e)
        {
            mDicts = mSugar.Queryable<SysDictTypeDto>().ToList();
            dgvDic.DataSource = mDicts;
        }
        private void dgvDic_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            var dict = mDicts[dgvDic.CurrentRow.Index];
            txtTargEnum.Text = dict.Code;
            mDictDatas = mSugar.Queryable<SysDictData>().Where(d => d.TypeId == dict.Id).ToList();
            dgvValue.DataSource = mDictDatas;
            txtTargEnum.Text=DicToEnumService.UnderScoresStyleToCamelStyle(dict.Code);
        }
        private void btnGo_Click(object sender, EventArgs e)
        {
            if(dgvDic.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择字典","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            if(string.IsNullOrEmpty(txtTargNamespace.Text))
            {
                MessageBox.Show("必须指定域名空间","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            if(mDictDatas==null|| !mDictDatas.Any())
            {
                return;
            }
            FolderBrowserDialog fd = new FolderBrowserDialog()
            {
                Description = "指定目标文件路径",
                ShowNewFolderButton = true,
                SelectedPath=txtTargPath.Text,
                
            };
            if(fd.ShowDialog()== DialogResult.OK)
            {
                txtTargPath.Text = fd.SelectedPath;
                try
                {
                    foreach (DataGridViewRow row in dgvDic.SelectedRows)
                    {
                        if(! mDicToEnumService.DictionaryToEnum(mDicts[row.Index].Code, txtTargPath.Text, txtTargNamespace.Text))
                        {
                            MessageBox.Show(mDicToEnumService.ErrMsg,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            
                        }

                    }
                    Process.Start("explorer.exe",fd.SelectedPath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
       

        
    }
}
